/**
 * 后台-前台菜单：自定义JS事件
 * */


//数据列表
function getIndex(){
    $.ajax({
        type: 'post',
        url: domain+'front/index',
        data: {},
        success: function (res) {
            res = JSON.parse(res);
            console.log(res);
            if (res.code!=0) {
                console.log(res.msg); return
            }
            getHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//列表字符串
function getHtml(d){
    var h = '', i, i2, id, title, childs, title2, code, cn, en, link;
    for (i=0; i<d.length; i++) {
        title = d[i].title, cn = d[i].cn, childs = d[i].childs;
        h += '<dl class="permission-list">';
        h += '<dt>';
        h += title;
        h += '</dt>';
        h += '<dd>';
        for (i2=0; i2<childs.length; i2++) {
            id = childs[i2].id, title2 = childs[i2].title, code = childs[i2].code;
            cn = childs[i2].cn, en = childs[i2].en, link = childs[i2].link;
            h += '<div style="margin-top:5px;">';
            h += '<b style="color:#777;font-size:16px;">'+title2+'：</b>';
            h += '<a href="javascript:;" style="color:#0088CC;" onclick="toEdit('+id+')">编辑</a>';
            h += '<br> &nbsp; &nbsp; &nbsp; &nbsp; ';
            h += '<span style="color:#777;">'+cn+'</span>';
            h += '<b style="color:#0088CC;"> || </b>';
            h += '<span style="color:#777;">'+en+'</span>';
            h += '<input type="hidden" id="cn'+id+'" value="'+cn+'"/>';
            h += '<input type="hidden" id="en'+id+'" value="'+en+'"/>';
            h += '<input type="hidden" id="link'+id+'" value="'+link+'"/>';
            h += '</div>';
        }
        h += '</dd>';
        h += '</dl>';
    }
    $('#tbody').html(h);
    //var h = '', id, title, childs, code, cn, en;
    //for (var i=0; i<d.length; i++) {
    //    id = d[i].id, title = d[i].title, childs = d[i].childs;
    //    h += '<dl class="permission-list">';
    //    h += '<dt>';
    //    h += title;
    //    h += '</dt>';
    //    h += '<dd>';
    //    for (var i2=0; i2<childs.length; i2++) {
    //        code = childs[i2].code, cn = childs[i2].cn, en = childs[i2].en;
    //        h += '<div style="margin-top:5px;">';
    //        h += '<a href="javascript:;" class="edit" onclick="toEdit('+code+')">编辑</a> &nbsp; ';
    //        h += '<span style="color:#777;">'+cn+'</span>';
    //        h += '<b style="color:#0088CC;"> || </b>';
    //        h += '<span style="color:#777;">'+en+'</span>';
    //        h += '<input type="hidden" id="cn'+code+'" value="'+cn+'"/>';
    //        h += '<input type="hidden" id="en'+code+'" value="'+en+'"/>';
    //        h += '</div>';
    //    }
    //    h += '</dd>';
    //    h += '</dl>';
    //}
    //$('#tbody').html(h);
}


//编辑
function toEdit(id){
    var cn = $('#cn'+id).val();
    var en = $('#en'+id).val();
    var link = $('#link'+id).val();
    var h = '';
    h += '<div style="position:fixed;top:0;left:0;z-index:10000;">';
    h += '<div style="width:100%;height:100%;min-height:500px;background:#000;opacity:0.2;position:fixed;top:0;left:0;z-index:10000;"></div>';
    h += '<div style="padding:10px 20px;min-width:100px;max-width:500px;text-align:center;background:#000;color:#FFF;border-radius:5px;position:fixed;bottom:40%;left:20%;z-index:10000;">';
    h += '<input type="hidden" id="id" value="'+id+'"> ';
    h += '<input type="text" id="cn" placeholder="输入中文" style="padding:5px 10px;width:100px;border:0;border-radius:3px;" value="'+cn+'"> ';
    h += '<input type="text" id="en" placeholder="输入英文" style="padding:5px 10px;width:100px;border:0;border-radius:3px;" value="'+en+'"> ';
    //h += '<br><input type="text" id="link" placeholder="输入地址" style="margin-top:5px;padding:5px 10px;width:225px;border:0;border-radius:3px;" value="'+link+'"> ';
    h += '<div style="margin:10px;">';
    h += '<input type="button" value="确定" style="padding:5px 20px;border:0;border-radius:2px;background:#0088CC;color:#FFF;cursor:pointer;" onclick="save()">  &nbsp; &nbsp;';
    h += '<input type="button" value="取消" style="padding:5px 20px;border:0;border-radius:2px;background:red;color:#FFF;cursor:pointer;" onclick="$(\'#pop\').html(\'\')"> ';
    h += '</div>';
    h += '</div>';
    h += '</div>';
    $('#pop').html(h);
}


//保存
function save(){
    var id = $('#id').val();
    var cn = $('#cn').val();
    var en = $('#en').val();
    var link = $('#link').val();
    if (!cn || !en) { pop({msg:'中文或英文不全'}); return }
    var formData  = {
        id: id, cn: cn, en: en, link: link,
    };
    $.ajax({
        type: 'post',
        url: domain+'front/modify',
        data: formData,
        success: function (res) {
            res = JSON.parse(res);
            console.log(res);
            if (res.code!=0) {
                pop({msg:res.msg}); return
            }
            pop({msg:'修改成功'});
            setTimeout(function(){
                window.location.href = '';
            },1000);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//前台多语言列表
function getLang(){
    $.ajax({
        type: 'post',
        url: domain+'lang/index',
        data: {},
        success: function (res) {
            res = JSON.parse(res);
            console.log(res);
            if (res.code!=0) {
                console.log(res.msg); return
            }
            getLangHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//语言列表字符串
function getLangHtml(d){
    var h = '', i, i2, id, title, childs, title2, code, cn, en, link;
    for (i=0; i<d.length; i++) {
        title = d[i].title, cn = d[i].cn, childs = d[i].childs;
        h += '<dl class="permission-list">';
        h += '<dt>';
        h += title+'页面';
        h += '</dt>';
        h += '<dd>';
        for (i2=0; i2<childs.length; i2++) {
            id = childs[i2].id, title2 = childs[i2].title, code = childs[i2].code;
            cn = childs[i2].cn, en = childs[i2].en, link = childs[i2].link;
            h += '<div style="margin-top:5px;">';
            h += '<a href="javascript:;" style="color:#0088CC;" onclick="toEdit('+id+')">编辑</a>： &nbsp; ';
            h += '<span style="color:#777;">'+cn+'</span>';
            h += '<b style="color:#0088CC;"> || </b>';
            h += '<span style="color:#777;">'+en+'</span>';
            h += '<input type="hidden" id="cn'+id+'" value="'+cn+'"/>';
            h += '<input type="hidden" id="en'+id+'" value="'+en+'"/>';
            h += '<input type="hidden" id="link'+id+'" value="'+link+'"/>';
            h += '</div>';
        }
        h += '</dd>';
        h += '</dl>';
    }
    $('#tbody').html(h);
}